from stock.utils.http_utils import http_get
from stock.model.stock_price import StockPrice


# 沪A  1
# 深圳0
# klt: int = 101,  # 行情之间的时间间隔 1、5、15、30、60分钟; 101:日; 102:周; 103:月
# fqt: int = 1, # 复权方式，0 不复权 1 前复权 2 后复权
# lmt=58
def get_stock_price(code, klt, count,end):
    url = 'https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64,f65&ut=fa5fd1943c7b386f172d6893dbfba10b&secid=%s&dect=1&klt=%s&lmt=%s&fqt=1&forcect=1&end=%s&wbp2u=1849325530509956|0|1|0|web&cb=__jp0' % (
    code, klt,count, end)
    ret = http_get(url, '')
    list = ret['data']['klines']
    ret_list = []
    for line in list:
        arr = line.split(',')
        date = arr[0]
        open = arr[1]
        close = arr[2]
        high = arr[3]
        low = arr[4]
        volume = arr[5]
        stockPrice = StockPrice(date, open, close, high, low, volume)
        ret_list.append(stockPrice)
    return ret_list


if __name__ == '__main__':
    code = '101.HG00Y'
    klt = '101'
    count = '10'
    end = '20501010'

    ret = get_stock_price(code,klt,count,end)
    for temp in ret:
        print(temp.date, temp.open, temp.close, temp.high, temp.low, temp.volume)
